﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.Shared
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.Profiles;
    using System.ComponentModel;
    using System.Web.Security;

    public partial class ThumbAvatar : System.Web.UI.UserControl
    {
        Guid _userid;
        bool _uidSet = false;
        public Guid UserId 
        {
            get
            {
                return _userid;
            }
            set
            {
                _userid = value;
                _uidSet = true;
            }
        }

        string _cssClass = string.Empty;
        bool _showName = true;
        bool _showOnlineStatus = true;

        public bool ShowOnlineStatus { get { return _showOnlineStatus; } set { _showOnlineStatus = value; } }
        public bool ShowNickName { get { return _showName; } set { _showName = value; } }
        public string CssClass { get { return _cssClass; } set { _cssClass = value; } }

        public ThumbAvatar()
        {
            //this.Load += new EventHandler(ThumbAvatar_Load);
            this.PreRender += new EventHandler(ThumbAvatar_PreRender);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (_cssClass != string.Empty)
            {
                pn1.CssClass += " " + _cssClass;
            }

            if (Context.User.Identity.IsAuthenticated && !_uidSet)
            {
                _userid = WebProfile.Current.UserId;
            }

            try
            {
                using (scDataContext cxt = new scDataContext())
                {
                    Sc_Profile p = cxt.Sc_Profiles.Single(d => d.UserId == _userid);
                    lnkImage.ImageUrl = ProfileOperations.GetAvatarImagePath(_userid, SharingCenter.Web.Classes.enums.AvatarType.Thumbnail, p) + "?" + DateTime.Now.Ticks;

                    lnkImage.NavigateUrl = string.Format("~/Home/?id={0}", _userid);

                    string tooltip = _userid == WebProfile.Current.UserId ? "我的主页" : "去{0}的主页";
                    lnkImage.ToolTip = string.Format(tooltip, p.NickName);

                    if (ShowNickName)
                    {
                        pnNickName.Visible = true;
                        if (_showOnlineStatus)
                        {
                            imgOnline.Visible = ((MembershipUser)Membership.GetUser(p.aspnet_Membership.aspnet_User.UserName)).IsOnline;
                        }
                        lnkUserName.Text = p.NickName == string.Empty ? " " : p.NickName;
                        lnkUserName.NavigateUrl = lnkImage.NavigateUrl;
                    }
                    else
                    {
                        pnNickName.Visible = false;
                    }
                }
            }
            catch { this.Visible = false; }

            base.Render(writer);
        }

        void ThumbAvatar_PreRender(object sender, EventArgs e)
        {
            
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}